package com.gxa.modules.service.propertyservice.Impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.propertymapper.EquipmentMapper;

import com.gxa.modules.pojo.*;
import com.gxa.modules.service.propertyservice.EquipmentService;
import com.gxa.modules.service.propertyservice.VisitorRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;


    /**
     * 门禁设备查询
     */

    @Override
    public R selectGuards(int page, int size,String deviceName,String deviceId,String guardsType) {

        R r = new R();
        int index=(page-1)*size;
        List<Guards> guards = equipmentMapper.selectGuards(index,size,deviceName,deviceId,guardsType);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(equipmentMapper.selectGuardssum(index,size,deviceName,deviceId,guardsType));
        r.setData(guards);
        return r;
    }

    @Override
    public R selectGuardsById(int id) {
        R r= new R();
        Guards guards = equipmentMapper.selectGuardsById(id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(guards);
        return r;
    }

    @Override
    public R addGuards(Guards guards) {
        R r = new R();
        int res = equipmentMapper.addGuards(guards);
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    /**
     * 根据id修改门禁状态
     * @param guardsTypeId
     * @return
     */
    @Override
    public R updateGuardsType(int guardsTypeId,int id) {
        R r = new R();
        int res = equipmentMapper.updateGuardsType(guardsTypeId,id);
        r.setCode(500);
        r.setMsg("修改失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    /**
     * 根据id删除门禁设备
     * @param id
     * @return
     */
    @Override
    public R deleteGuardsById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(equipmentMapper.deleteGuardsById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }

    /**
     * 批量删除门禁设备
     * @param list
     */
    @Override
    public void batchDelete(List<Integer> list) {
        equipmentMapper.batchDelete(list);
    }

    @Override
    public R deleteGuards_patrolById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(equipmentMapper.deleteGuards_patrolById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;

    }

    @Override
    public R selectGuardsPatrolById(int id) {

        R r= new R();
        GuardsPatrol guardsPatrol = equipmentMapper.selectGuardsPatrolById(id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(guardsPatrol);
        return r;
    }

    /**
     * 门禁报警查询
     */
    @Override
    public R selectGuards_alarm(int page, int size, String deviceName,String deviceId,String guardsAlarmType) {

        int index=(page-1)*size;
        R r = new R();
        List<GuardsAlarm> guardsAlarm = equipmentMapper.selectGuards_alarm(index,size,deviceName,deviceId,guardsAlarmType);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(equipmentMapper.selectGuards_alarmsum(index,size,deviceName,deviceId,guardsAlarmType));
        r.setData(guardsAlarm);
        return r;
    }


    @Override
    public R selectGuardsAlarmById(int id) {
        R r= new R();
        GuardsAlarm guardsAlarm = equipmentMapper.selectGuardsAlarmById(id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(guardsAlarm);
        return r;
    }

    /**
     * 根据id删除门禁报警设备
     */
    @Override
    public R deleteGuards_alarmById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(equipmentMapper.deleteGuards_alarmById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }

    /**
     * 查询门禁巡视记录
     */
    @Override
    public R selectGuards_patrol(int page, int size, String deviceName,String deviceId,String patrolType) {
        R r = new R();
        int index = (page-1)*size;
        List<GuardsPatrol> guardsPatrols = equipmentMapper.selectGuards_patrol(index, size, deviceName, deviceId,patrolType);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(equipmentMapper.selectGuards_patrolSum(index, size, deviceName, deviceId,patrolType));
        r.setData(guardsPatrols);
        return r;
    }
}
